package com.ruoyi.youtian.controller;

import java.math.BigDecimal;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.youtian.domain.TEarlyWarningRecordDetail;
import com.ruoyi.youtian.service.ITEarlyWarningRecordDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 触发预警记录详情Controller
 * 
 * @author ruoyi
 * @date 2022-10-17
 */
@Controller
@RequestMapping("/youtian/failureRateMonitor")
public class TEarlyWarningRecordDetailController extends BaseController
{
    private String prefix = "youtian/failureRateMonitor";

    @Autowired
    private ITEarlyWarningRecordDetailService tEarlyWarningRecordDetailService;

    @RequiresPermissions("youtian:failureRateMonitor:view")
    @GetMapping()
    public String failureRateMonitor()
    {
        return prefix + "/failureRateMonitor";
    }

    /**
     * 查询触发预警记录详情列表
     */
    @RequiresPermissions("youtian:failureRateMonitor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody TEarlyWarningRecordDetail tEarlyWarningRecordDetail)
    {
        startPage();
        List<TEarlyWarningRecordDetail> list = tEarlyWarningRecordDetailService.selectTEarlyWarningRecordDetailList(tEarlyWarningRecordDetail);
        list.forEach(t->{
            if (t.getFailureGrowthRate().toBigInteger().bitCount() == 0) {
                t.setFailureGrowthRate(BigDecimal.valueOf(1));
            }
            if (t.getFailureRate().toBigInteger().bitCount() == 0) {
                t.setFailureRate(BigDecimal.valueOf(2));
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出触发预警记录详情列表
     */
    @RequiresPermissions("youtian:failureRateMonitor:export")
    @Log(title = "触发预警记录详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TEarlyWarningRecordDetail tEarlyWarningRecordDetail)
    {
        List<TEarlyWarningRecordDetail> list = tEarlyWarningRecordDetailService.selectTEarlyWarningRecordDetailList(tEarlyWarningRecordDetail);
        ExcelUtil<TEarlyWarningRecordDetail> util = new ExcelUtil<TEarlyWarningRecordDetail>(TEarlyWarningRecordDetail.class);
        return util.exportExcel(list, "触发预警记录详情数据");
    }

    /**
     * 新增触发预警记录详情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存触发预警记录详情
     */
    @RequiresPermissions("youtian:failureRateMonitor:add")
    @Log(title = "触发预警记录详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TEarlyWarningRecordDetail tEarlyWarningRecordDetail)
    {
        return toAjax(tEarlyWarningRecordDetailService.insertTEarlyWarningRecordDetail(tEarlyWarningRecordDetail));
    }

    /**
     * 修改触发预警记录详情
     */
    @RequiresPermissions("youtian:failureRateMonitor:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TEarlyWarningRecordDetail tEarlyWarningRecordDetail = tEarlyWarningRecordDetailService.selectTEarlyWarningRecordDetailById(id);
        mmap.put("tEarlyWarningRecordDetail", tEarlyWarningRecordDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存触发预警记录详情
     */
    @RequiresPermissions("youtian:failureRateMonitor:edit")
    @Log(title = "触发预警记录详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TEarlyWarningRecordDetail tEarlyWarningRecordDetail)
    {
        return toAjax(tEarlyWarningRecordDetailService.updateTEarlyWarningRecordDetail(tEarlyWarningRecordDetail));
    }

    /**
     * 删除触发预警记录详情
     */
    @RequiresPermissions("youtian:failureRateMonitor:remove")
    @Log(title = "触发预警记录详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tEarlyWarningRecordDetailService.deleteTEarlyWarningRecordDetailByIds(ids));
    }
}
